package com.zzb.servlet;


import com.zzb.model.NewsModel;
import com.zzb.model.NewsTypeModel;
import com.zzb.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet("/news")
public class NewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.处理乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

        //2.得到前端数据

        //3.将数据封装到model


        //4.处理服务逻辑关系
        //新闻
        NewsService newsService = new NewsService();

        //5.得到处理返回值
        //得到新闻类型
        List<NewsTypeModel> newsTypeModelList = newsService.getNewsTypeModelList();
        //得到新闻内容
        List<NewsModel> indexNewsModelList = newsService.getNewsModelList("媒体报道");


        req.setAttribute("newsTypeModelList",newsTypeModelList);
        req.setAttribute("indexNewsModelList",indexNewsModelList);

        //6.将数据返回到前端
        req.getRequestDispatcher("/jsp/news.jsp").forward(req, resp);
    }
}
